/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using BlindShark.GameElements;

using SdlDotNet.Graphics;

namespace BlindShark.Widgets
{
	/// <summary>
	/// Description of PlayerCard.
	/// </summary>
	public class PlayerCard : IWidget
	{
		Surface _sfPlayerCard;
		Surface _sfAvatar;
		Rectangle _rctPlayerCard;
		
		Label _lblName;
		Label _lblScore;
		
		Player _plPlayer;
		
		bool _bShowScore;
		
		public PlayerCard(Player player)
		{
			_plPlayer = player;
			_bShowScore = true;
			
			// Initializing the labels
			_lblName = new Label(_plPlayer.Name, 16, Color.Yellow, TextAlignment.CenterMiddle);
			_lblScore = new Label("", 24, Color.AntiqueWhite, TextAlignment.CenterMiddle);
		}
		
		#region IWidget interface Members
		public Surface Surface
		{
			get {return _sfPlayerCard;}
		}
		
		public Point Location
		{
			get {return _rctPlayerCard.Location;}
			set {_rctPlayerCard.Location = value;}
		}
		
		public void Init()
		{
			InitCard();
			_rctPlayerCard.Size = _sfPlayerCard.Size;
			
			// Initializing the labels
			_lblName.Size = new Size(_rctPlayerCard.Width, 24);
			_lblName.Location = new Point(0,106);
			_lblName.Init();
			if (_bShowScore)
			{
				_lblScore.Text = _plPlayer.Score.ToString();
				_lblScore.Size = new Size(44, 34);
				_lblScore.Location = new Point(90, 148);
				_lblScore.Init();
			}
				
			// Getting the avatar
			if (_sfAvatar == null)
				_sfAvatar = SurfaceManager.GetSurface("avatars/"+_plPlayer.Avatar).CreateStretchedSurface(new Size(76,76));
			
			_sfPlayerCard.Blit(_sfAvatar, new Point(37,22));
			_sfPlayerCard.Blit(_lblName.Surface, _lblName.Location);
			if (_bShowScore)
				_sfPlayerCard.Blit(_lblScore.Surface, _lblScore.Location);
		}
		
		public void HandleEvents()
		{
			
		}
		
		public void CleanUp()
		{
			_sfPlayerCard = null;
			_sfAvatar = null;
			
			_lblName.CleanUp();
			_lblScore.CleanUp();
		}
		#endregion
		
		public void Refresh()
		{
			Init();
		}
		
		#region Drawing functions
		private void InitCard()
		{
			switch(_plPlayer.State)
			{
				case PlayerState.answering:
				case PlayerState.inGame:
					_sfPlayerCard = new Surface(SurfaceManager.GetSurface("gfx/playerOK.png"));
					_sfPlayerCard.Blit(SurfaceManager.GetSurface("gfx/playerOK.png"));
					break;
				case PlayerState.outGame:
					_sfPlayerCard = new Surface(SurfaceManager.GetSurface("gfx/playerNOK.png"));
					_sfPlayerCard.Blit(SurfaceManager.GetSurface("gfx/playerNOK.png"));
					break;
			}
		}
		#endregion
		
		#region Accessors
		public bool ShowScore
		{
			get {return _bShowScore;}
			set {_bShowScore = value;}
		}
		
		public Size Size
		{
			get {return _rctPlayerCard.Size;}
			set {_rctPlayerCard.Size = value;}
		}
		
		public int Width
		{
			get {return _rctPlayerCard.Width;}
			set {_rctPlayerCard.Width = value;}
		}
		
		public int Height
		{
			get {return _rctPlayerCard.Height;}
			set {_rctPlayerCard.Height = value;}
		}
		
		public int X
		{
			get {return _rctPlayerCard.X;}
			set {_rctPlayerCard.X = value;}
		}
		
		public int Y
		{
			get {return _rctPlayerCard.Y;}
			set {_rctPlayerCard.Y = value;}
		}
		
		public int Bottom
		{
			get {return _rctPlayerCard.Bottom;}
		}
		
		public int Right
		{
			get {return _rctPlayerCard.Right;}
		}
		
		public Player Player
		{
			get {return _plPlayer;}
		}
		#endregion
	}
}
